<template>
  <div>
    <el-form
      ref="ruleFormRef"
      style="max-width: 900px"
      :model="ruleForm"
      :rules="rules"
      label-width="auto"
      class="demo-ruleForm"
      :size="formSize"
      status-icon
    >
      <el-form-item label="销售渠道" prop="resource">
        <el-radio-group v-model="ruleForm.resource">
          <el-radio value="Sponsorship">线上零售销售</el-radio>
          <el-radio value="Venue">门店销售</el-radio>
          <el-radio value="Venue">线上批发</el-radio>
        </el-radio-group>
      </el-form-item>
      <div class="form-item-line">
        <el-form-item label="真实库存" prop="name">
          <el-input v-model="ruleForm.name" disabled />
          <span class="lable">直实库存从仓库同步，不可修改</span>
        </el-form-item>
        <el-form-item label="最低售价" prop="name" class="item">
          <el-input v-model="ruleForm.name" disabled />
          <span class="lable">此价格是公司的保底价，任何售价不能底于止价。</span>
        </el-form-item>
      </div>

      <el-form-item label="商品规格" prop="resource">
        <el-radio-group v-model="ruleForm.resource">
          <el-radio value="Sponsorship">单规格</el-radio>
          <el-radio value="Venue">多规格</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="图片" prop="region">
        <img src="" class="img" />
      </el-form-item>
      <div class="form-item-line">
        <el-form-item label="售价" prop="name">
          <el-input v-model="ruleForm.name" />
        </el-form-item>
        <el-form-item label="成本价" prop="name">
          <el-input v-model="ruleForm.name" />
        </el-form-item>
        <el-form-item label="会员价" prop="name">
          <el-input v-model="ruleForm.name" />
        </el-form-item>
      </div>

      <div class="form-item-line">
        <el-form-item label="活动价" prop="name">
          <el-input v-model="ruleForm.name" />
        </el-form-item>
        <el-form-item label="折扣价" prop="name">
          <el-input v-model="ruleForm.name" />
        </el-form-item>
        <el-form-item label="销售库存" prop="name">
          <el-input v-model="ruleForm.name" />
        </el-form-item>
      </div>

      <div class="form-item-line">
        <el-form-item label="商品编号" prop="name">
          <el-input v-model="ruleForm.name" />
        </el-form-item>
        <el-form-item label="重量" prop="name">
          <el-input v-model="ruleForm.name" />
        </el-form-item>
        <el-form-item label="体积" prop="name">
          <el-input v-model="ruleForm.name" />
        </el-form-item>
      </div>

      <el-form-item label="备注" prop="desc">
        <el-input v-model="ruleForm.desc" type="textarea" />
      </el-form-item>
      <el-form-item class="button-fix">
        <el-button @click="resetForm(ruleFormRef)">上一步</el-button>
        <el-button type="primary" @click="submitForm(ruleFormRef)"> 下一步 </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue'
import type { ComponentSize, FormInstance, FormRules } from 'element-plus'

interface RuleForm {
  name: string
  region: string
  count: string
  date1: string
  date2: string
  delivery: boolean
  location: string
  type: string[]
  resource: string
  desc: string
}

const formSize = ref<ComponentSize>('default')
const ruleFormRef = ref<FormInstance>()
const ruleForm = reactive<RuleForm>({
  name: '0.00',
  region: '',
  count: '',
  date1: '',
  date2: '',
  delivery: false,
  location: '',
  type: [],
  resource: '',
  desc: ''
})

const locationOptions = ['Home', 'Company', 'School']

const rules = reactive<FormRules<RuleForm>>({
  name: [
    { required: true, message: 'Please input Activity name', trigger: 'blur' },
    { min: 3, max: 5, message: 'Length should be 3 to 5', trigger: 'blur' }
  ],
  region: [
    {
      required: true,
      message: 'Please select Activity zone',
      trigger: 'change'
    }
  ],
  count: [
    {
      required: true,
      message: 'Please select Activity count',
      trigger: 'change'
    }
  ],
  date1: [
    {
      type: 'date',
      required: true,
      message: 'Please pick a date',
      trigger: 'change'
    }
  ],
  date2: [
    {
      type: 'date',
      required: true,
      message: 'Please pick a time',
      trigger: 'change'
    }
  ],
  location: [
    {
      required: true,
      message: 'Please select a location',
      trigger: 'change'
    }
  ],
  type: [
    {
      type: 'array',
      required: true,
      message: 'Please select at least one activity type',
      trigger: 'change'
    }
  ],
  resource: [
    {
      required: true,
      message: 'Please select activity resource',
      trigger: 'change'
    }
  ],
  desc: [{ required: true, message: 'Please input activity form', trigger: 'blur' }]
})

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log('submit!')
    } else {
      console.log('error submit!', fields)
    }
  })
}

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}

const options = Array.from({ length: 10000 }).map((_, idx) => ({
  value: `${idx + 1}`,
  label: `${idx + 1}`
}))
</script>

<style scoped lang="scss">
.form-item-line {
  display: flex;
  justify-content: space-between;
  .item {
    padding-left: 30px;
  }
  .lable {
    font-size: 12px;
  }
}
.img {
  width: 120px;
  height: 120px;
  background: #eee;
  border: 1px solid #eee;
}
</style>
